#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
合并所有 citing_articles_formatted_xx.txt 文件为一个 citing_articles.txt 文件
"""

import os
import glob
import re
from pathlib import Path

def natural_sort_key(filename):
    """
    自然排序键函数，用于正确排序文件名中的数字
    例如：1, 2, 3, 11, 100 而不是 1, 100, 11, 2, 3
    """
    # 提取文件名中的数字部分
    parts = re.findall(r'\d+', filename)
    if parts:
        # 如果是范围格式（如 11-40），取第一个数字
        return int(parts[0])
    return 0

def merge_citing_articles():
    """合并所有 citing_articles_formatted_xx.txt 文件"""
    
    # 设置路径
    data_dir = Path("data")
    output_file = data_dir / "citing_articles.txt"
    
    # 查找所有 citing_articles_formatted_xx.txt 文件
    pattern = str(data_dir / "citing_articles_formatted_*.txt")
    files = glob.glob(pattern)
    
    if not files:
        print("未找到任何 citing_articles_formatted_*.txt 文件")
        return
    
    # 按自然顺序排序文件
    files.sort(key=lambda x: natural_sort_key(os.path.basename(x)))
    
    print(f"找到 {len(files)} 个文件需要合并:")
    for file in files:
        print(f"  - {os.path.basename(file)}")
    
    # 合并文件
    total_lines = 0
    with open(output_file, 'w', encoding='utf-8') as outfile:
        for i, file_path in enumerate(files):
            print(f"正在处理 {os.path.basename(file_path)}...")
            
            try:
                with open(file_path, 'r', encoding='utf-8') as infile:
                    lines = infile.readlines()
                    
                    # 写入内容
                    for line in lines:
                        if line.strip():  # 跳过空行
                            outfile.write(line)
                            total_lines += 1
                    
                print(f"  已处理 {len(lines)} 行")
                
            except Exception as e:
                print(f"  处理文件 {file_path} 时出错: {e}")
                continue
    
    print(f"\n合并完成！")
    print(f"输出文件: {output_file}")
    print(f"总计 {total_lines} 行数据")
    
    # 显示合并后文件的统计信息
    if output_file.exists():
        file_size = output_file.stat().st_size
        print(f"文件大小: {file_size:,} 字节")
        
        # 统计不同类型的记录
        with open(output_file, 'r', encoding='utf-8') as f:
            lines = f.readlines()
            
        valid_urls = 0
        no_citation = 0
        
        for line in lines:
            if line.strip():
                parts = line.strip().split(',')
                if len(parts) >= 3:
                    if parts[2] == '无引用文章':
                        no_citation += 1
                    else:
                        valid_urls += 1
        
        print(f"有效引用URL: {valid_urls}")
        print(f"无引用文章: {no_citation}")

if __name__ == "__main__":
    merge_citing_articles()